
# REQUIRES: Data/Framing the Exit Experiment Raw Data.csv

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Experiment/Cleaning.R")

################# Figure S3 - Interaction of Middle Ground Frame and Identifying as a Political Moderate #################
### H3c
# H3c: The effect of the middle ground treatment will be more strongly positive (on support for staying the course) among political moderates.
# logit staycourse_yn [interact framing treatment with the middle category of the political ideology scale]
p1<- data[(data$political_affiliation != "8"),]
p1 <- p1[!is.na(p1$id), ]
p1$moderate <- ifelse(p1$political_affiliation == "4", 1, 0)

p1$f_mod <- p1$framing*p1$moderate

h3c <- glm(staycourse_yn ~ framing*moderate, data=p1, family="binomial")
summary(h3c)
h3c %>% coef() %>% exp()

# Evaluated at means
allmean <- data.frame(framing=mean(p1$framing),
                      moderate=mean(p1$moderate),
                      f_mod=mean(p1$f_mod))
allmean$pred.prob <- predict(h3c, newdata=allmean, type="response")
allmean <- cbind(allmean,predict(h3c, newdata=allmean, type="response", se.fit=TRUE))
allmean

# Evaluated at means of moderate and f_mod by middle framing condition
h3a2 <- glm(staycourse_yn ~ framing_recode+moderate+f_mod, data=p1, family="binomial")
summary(h3a2)
allmean2 <- data.frame(moderate=rep(mean(p1$moderate),2),
                       f_mod=rep(mean(p1$f_mod),2),
                       framing_recode=as.factor(c('reduce', 'increase')))
allmean2 <- cbind(allmean2,predict(h3a2, newdata=allmean2, type="response", se.fit=TRUE))
allmean2

logitmfx(staycourse_yn ~ framing*moderate, data=p1)

### Figure 5
# Interplot
margin <- unit(0.5, "line")
grob1 <- interplot(m = h3c, var1 = "framing", var2 = "moderate") +
  theme_light() +
  theme(text=element_text(family="Times New Roman"), axis.text.x=element_blank()) +
  #scale_x_discrete(labels = c("Extemely Liberal", "Liberal", "Slightly Liberal", "Moderate", "Slightly Conservative", "Conservative", "Extremely Conservative")) +
  labs(y = "Marginal Effect on Support for Staying the Course") +
  geom_hline(yintercept = 0, linetype = "dashed")
grob2 <- textGrob("   ", gp=gpar(fontsize=11, fontfamily="Times New Roman"))
grob3 <- textGrob("Participant is a Moderate", gp=gpar(fontsize=11, fontfamily="Times New Roman"))
grob4 <- textGrob("Figure S3: Interaction of Middle Ground Frame and Identifying as a Political Moderate", gp=gpar(fontsize=13, fontfamily="Times New Roman"))

plot5 <- grid.arrange(grob1, grob2, grob3, grob4, 
                      heights = unit.c(unit(1,"null"), grobHeight(grob2) + 1.2*margin, grobHeight(grob3) + 1.2*margin, grobHeight(grob4) + margin),
                      vp=viewport(width=0.95, height=0.95))

ggsave("Plots/Figure S3 - Interaction of Middle Ground Frame and Identifying as a Political Moderate.png", plot5)

rm(allmean, allmean2, grob1, grob2, grob3, grob4, h3a2, h3c, p1, plot5, margin)
